#!/bin/bash

# 2014-09-07

# Black       0;30     Dark Gray     1;30
# Blue        0;34     Light Blue    1;34
# Green       0;32     Light Green   1;32
# Cyan        0;36     Light Cyan    1;36
# Red         0;31     Light Red     1;31
# Purple      0;35     Light Purple  1;35
# Brown       0;33     Yellow        1;33
# Light Gray  0;37     White         1;37


# PS1="\[\e[0;32m\]\u\[\e[m\] \[\e[1;34m\]\w\[\e[m\] \[\e[1;32m\]\$\[\e[m\] \[\e[1;37m\] \[\e[0m\]"
#cyellow () { 
#	echo -n "\[\e[1;33m\]$@\[\e[0m\]" 
#}

alias ccze="ccze -A"
alias ls="ls --group-directories-first --color=auto"

unalias ll
function ll ()
{
        ls -hlvB --dereference $@ | ccze
}

unalias la
function la ()
{
	ll --almost-all $@ | ccze
}

unalias dir
function dir () 
{
	dir -hlvB --color=auto --almost-all --group-directories-first --dereference --author $@ | ccze
}


function mtr ()
{
	/usr/bin/mtr --report --report-wide $@ | ccze
}



function ipt ()
{
	/usr/sbin/iptables $@ | ccze
}


function df ()
{

	/usr/bin/df $@ 2>/dev/null | ccze
}

function userlist ()
{
    [ "$EUID" == 0 ] || return 255

	local userlist="$(mktemp /dev/shm/userlist.XXXXX)"
	echo "UID:GID:USERNAME:SHELL:HOME:GECOS" > ${userlist}
	echo "---:---:--------:-----:----:-----" >> ${userlist}
	cat /etc/passwd | awk -F: '{ printf "%d:%d:%s:%s:%s:%s\n", $3, $4, $1, $7, $6, $5 }' | sort -h >> ${userlist}
	column -ts: ${userlist}
	rm ${userlist}
}


#function _homesize ()
#{
#    echo -n `du -hs $1 | awk '{ print $1 }'`
#}


function luserlist ()
{
    [ "$EUID" == 0 ] || return 255

    function _homesize ()
    {
        echo -n `du -hs $1 | awk '{ print $1 }'`
    }


    local passlist="$(mktemp /dev/shm/passlist.XXXXX)"
    local userlist="$(mktemp /dev/shm/userlist.XXXXX)"
    local uidmin="`grep -P '^UID_MIN' /etc/login.defs | awk '{ print $2 }'`"
    local uidmax="`grep -P '^UID_MAX' /etc/login.defs | awk '{ print $2 }'`"

    # echo "userlist = $userlist"
    # echo "passlist = $passlist"
    # echo "uidmin = $uidmin"
    # echo "uidmax = $uidmax"
    
    
    echo "UID:GID:USERNAME:SHELL:HOME:SPACE_USED" > ${userlist}
    echo "---:---:--------:-----:----:----------" >> ${userlist}
    cat /etc/passwd | awk -F: '{ printf "%d:%d:%s:%s:%s:%%homesize%%\n", $3, $4, $1, $7, $6 }' | sort -h > ${passlist}

    # here's my nifty multi-line perl one-liner :)
    # print out usernames starting from the system min to system max user ids
    perl -F: -i -lane  '{ BEGIN { $low = shift; $high = shift;  }; print $_ if ( $F[0] >= $low && $F[0] <= $high ); }' \
     $uidmin $uidmax ${passlist}

    for x in  $(awk -F: '{ print $5 }' ${passlist}); do
        size=$(_homesize $x);
        sed -i -e 's/:$x:%homesize%/:$x:$size/' ${passlist}
    done

     cat ${passlist} >> ${userlist}
     rm ${passlist}
     cat ${userlist} | column -ts:
     rm ${userlist}

}


function usermod_auto_nologin ()
{
	[ "$EUID" == 0 ] || return 255
	local users="`grep /bin/false /etc/passwd | cut -d: -f1`"
	echo  "Changing shells from /bin/false to /usr/sbin/nologin for the following users:"
	for x in $users; do
		echo -n "* ${x}		"
		usermod -s /usr/sbin/nologin $x 
		if [ "$?" == 0 ]; then
			echo "Ok."
		else
			echo "Failed."
		fi
	done
	echo "Done."
}
